package LeetCode;

/**
 * Created by kechelle on 2020/8/12 14:05
 * 两数相加
 * 示例：
 *
 * 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出：7 -> 0 -> 8
 * 原因：342 + 465 = 807
 */
public class Code2 {

    public class ListNode{
        int val;
        ListNode next;
        ListNode(int x){
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode l1,ListNode l2){
        return addNumber(l1,l2,0);
    }

    private ListNode addNumber(ListNode l1, ListNode l2, int i) {
        if (l1==null&&l2==null&&i==0) return null;
        if (l1==null){
           l1 = new ListNode(0);
        }
        if (l2==null){
            l2 = new ListNode(0);
        }
        //输出结果为链表
        int num = l1.val + l2.val + i;
        ListNode result = new ListNode(num%10);
        result.next = addNumber(l1.next,l2.next,num/10);
        return result;
    }
}
